package com.yao.code;

import com.sun.jmx.remote.internal.ArrayQueue;
import com.yao.tool.TreeNode;
import sun.reflect.generics.tree.Tree;

import java.util.*;

/**
 * @author yaoquanwei
 * @date 2021/8/13 08:31
 */
public class Code210813 {
    /**
     * 剑指offer 32.从上到下打印二叉树 2
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (root == null) {
            return res;
        }
        List<Integer> list;
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            list = new ArrayList<Integer>();
            while (size > 0) {
                TreeNode tem = queue.poll();
                list.add(tem.val);
                if (tem.left != null) {
                    queue.offer(tem.left);
                }
                if (tem.right != null) {
                    queue.offer(tem.right);
                }
                size--;
            }
            res.add(list);
        }

        return res;
    }

    /**
     * 剑指offer 32.从上到下打印二叉树 1
     *
     * @param root
     * @return
     */
    public int[] levelOrder1(TreeNode root) {
        List<Integer> list = new ArrayList();
        if (root == null) {
            return list.stream().mapToInt(Integer::valueOf).toArray();
        }
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.offer(root);
        list.add(root.val);
        while (!queue.isEmpty()) {
            TreeNode tem = queue.poll();
            if (tem.left != null) {
                queue.offer(tem.left);
                list.add(tem.left.val);
            }
            if (tem.right != null) {
                queue.offer(tem.right);
                list.add(tem.right.val);
            }
        }

        return list.stream().mapToInt(Integer::valueOf).toArray();
    }

    /**
     * 剑指offer 32.从上到下打印二叉树 3
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder3(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        // 记录每层的数字
        List<Integer> list;
        queue.offer(root);
        // 记录第几层
        int floor = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            list = new ArrayList<Integer>();
            while (size > 0) {
                TreeNode tem = queue.poll();
                list.add(tem.val);
                if (tem.left != null) {
                    queue.offer(tem.left);
                }
                if (tem.right != null) {
                    queue.offer(tem.right);
                }
                size--;
            }
            // 偶数层正序排列
            if (floor % 2 == 0) {
                res.add(list);
            } else {
                // 奇数层倒叙排列
                Collections.reverse(list);
                res.add(list);
            }
            floor++;
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(new int[0]);
    }
}
